<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/column1';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	
	public $general = array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
	
	public function init()
	{
		$this->initGeneralInfo();
	}
	
	public function getNavMenus()
	{
		return new CActiveDataProvider('Menu', array(
			'criteria'=>array(
				'condition'=>'visible=1 and show_type = 1'
			),
			'pagination'=>array(
				'pageSize'=>100
			)
		));
	}
	
	public function getHeadMenus()
	{
		return new CActiveDataProvider('Menu', array(
			'criteria'=>array(
				'condition'=>'show_type = 2'
			),
			'pagination'=>array(
				'pageSize'=>100
			)
		));
	}
	
	public function getRoomList()
	{
		return new CActiveDataProvider('Room', array(
			'criteria'=>array(
				'condition'=>''
			),
			'pagination'=>array(
				'pageSize'=>100
			)
		));
	}
	
	public function getIndexFavs($size)
	{
		return new CActiveDataProvider('Fav', array(
			'criteria'=>array(
				'condition'=>'show_index = 1'
			),
			'pagination'=>array(
				'pageSize'=>$size
			)
		));
	}
	
	public function initGeneralInfo()
	{
		$gList = new CActiveDataProvider('General', array(
			'criteria'=>array(
				'condition'=>''
			),
			'pagination'=>array(
				'pageSize'=>1000
			)
		));
		$list = $gList->getData();
		if (!empty($list))
		{
			foreach ($list as $key => $value) {
				$this->general[$value["key"]]=$value;
			}
		}
	}
	
	public function getUserModel() {
		$model = new User();
		$model->gender = '1';
		return $model;
	}
	
	public function getLoginForm()
	{
		$model = new LoginForm;
		return $model;
	}
	
	function utf_substr($str,$len)
	{
		for($i=0;$i<$len;$i++)
		{
			$temp_str=substr($str,0,1);
			if(ord($temp_str) > 127)
			{
				$i++;
				if($i<$len)
				{
					$new_str[]=substr($str,0,3);
					$str=substr($str,3);
				}
			}
			else
			{
				$new_str[]=substr($str,0,1);
				$str=substr($str,1);
			}
		}
		return join($new_str);
	}
}